// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.chatkit.ui.view.message.viewholder;

import android.graphics.Color;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;

import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.yunxin.kit.chatkit.ui.R;
import com.netease.yunxin.kit.chatkit.ui.common.MessageUtil;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatMessageTextViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean;
import com.netease.yunxin.kit.chatkit.ui.view.message.MessageProperties;

/**
 * view holder for Text message
 */
public class ChatTextMessageViewHolder extends ChatBaseMessageViewHolder {

    ChatMessageTextViewHolderBinding textBinding;

    public ChatTextMessageViewHolder(@NonNull ChatBaseMessageViewHolderBinding parent, int viewType) {
        super(parent, viewType);
    }

    @Override
    public void addContainer() {
        textBinding = ChatMessageTextViewHolderBinding.inflate(LayoutInflater.from(parent.getContext()), getContainer(), true);
    }

    @Override
    public void bindData(ChatMessageBean message, ChatMessageBean lastMessage) {
        super.bindData(message, lastMessage);
        if (properties.getMessageTextSize() != MessageProperties.INT_NULL) {
            textBinding.messageText.setTextSize(properties.getMessageTextSize());
        }
        if (properties.getMessageTextColor() != MessageProperties.INT_NULL) {
            textBinding.messageText.setTextColor(properties.getMessageTextColor());
        }
        if (message.getMessageData().getMessage().getDirect() == MsgDirectionEnum.In) {
            //我收到的
            textBinding.messageText.setTextColor(Color.parseColor("#424242"));
        } else {
            //我发送的
            textBinding.messageText.setTextColor(Color.WHITE);
        }
        if (message.getMessageData().getMessage().getMsgType() == MsgTypeEnum.text) {
            MessageUtil.identifyFaceExpression(textBinding.getRoot().getContext(), textBinding.messageText, message.getMessageData().getMessage().getContent(), ImageSpan.ALIGN_BOTTOM);
        } else {
            //文件消息暂不支持所以展示提示信息
            textBinding.messageText.setText(parent.getContext().getResources().getString(R.string.chat_message_not_support_tips));
        }
    }
}
